import vue from '../main'
import store from '../store/index';
import { Loading, Notification } from 'element-ui';
import { OBJ } from './init'
import { PARAMS } from "./params"
import { checkStr, noticeAndMessige } from './methods'

/**
 * 显示分支模态框
 * flag :true显示  :false隐藏
 * **/
export function showBranch(flag){
    if(flag){
        store.commit('switchBranch', true);
    }else{
        store.commit('switchBranch', false);
    }
}

//查询分支
export function queryBranch(){
    let queryFrameId = {
        op: "dbparam",
        topic: "query",
        name: ""
    }
    let queryMapMsg = new ROSLIB.Message({
        seq: 1,
        frame_id: JSON.stringify(queryFrameId)
    })
    OBJ.topic.taskSwitchTopic.publish(queryMapMsg);
}

//新建分支
export function newBranch(branchName){
    if (branchName == "") {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.nonEmpty"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return;
    }

    if (!checkStr(branchName)) {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.theNameUnavailable"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return;
    }
    Loading.service({
        text: vue.$t("loading")
    })
    let addFrameId = {
        op: "dbparam",
        topic: "add",
        name: branchName
    }
    let addBranchMsg = new ROSLIB.Message({
        seq: 1,
        frame_id: JSON.stringify(addFrameId)
    })
    OBJ.topic.taskSwitchTopic.publish(addBranchMsg);
    Loading.service().close();
    return true;
}

//切换分支
export function switchBranch(name){
    Loading.service({
        text: vue.$t("inTheSwitch")
    })
    let selectFrameId = {
        op: "dbparam",
        topic: "select",
        name: name
    }
    let selectMapMsg = new ROSLIB.Message({
        seq: 1,
        frame_id: JSON.stringify(selectFrameId)
    })
    console.log(selectMapMsg)
    OBJ.topic.taskSwitchTopic.publish(selectMapMsg);
    Loading.service().close();
};

//删除分支
export function deleteBranch(name){
    Loading.service({
        text: vue.$t("deleting")
    });

    let removeFrameId = {
        op: "dbparam",
        topic: "remove",
        name: name
    }
    let removeMapMsg = new ROSLIB.Message({
        seq: 1,
        frame_id: JSON.stringify(removeFrameId)
    })
    console.log(removeMapMsg)
    OBJ.topic.taskSwitchTopic.publish(removeMapMsg);
    Loading.service().close();
};


/**
 * >>>git       分支的增删改查状态回调
 * hardware_id  增删改查关键词
 * level        0成功，1失败
 * message      成功失败信息
 * **/
export function gitMapMethods(...params){
    let {hardware_id, level, message} = params[0];
    console.log(params)
    switch (hardware_id){
        case PARAMS.gitMethods.add://增加
            if(level === 0){
                noticeAndMessige({
                    title: vue.$t("titles.success"),
                    message: vue.$t("messages.addSuccess"),
                    type: "success"
                })
            }else if(level === 1){
                noticeAndMessige({
                    title: vue.$t("titles.success"),
                    message: message,
                    type: "error"
                })
            }
            break;
        case PARAMS.gitMethods.remove://删除
            if(level === 0){
                noticeAndMessige({
                    title: vue.$t("titles.success"),
                    message: vue.$t("messages.removeSuccess"),
                    type: "success"
                })
            }else if(level === 1){
                noticeAndMessige({
                    title: vue.$t("titles.error"),
                    message: message,
                    type: "error"
                })
            }
            break;
        case PARAMS.gitMethods.select://切换
            if(level === 0){
                noticeAndMessige({
                    title: vue.$t("titles.success"),
                    message: vue.$t("messages.checkoutSuccess"),
                    type: "success"
                })
            }else if(level === 1){
                noticeAndMessige({
                    title: vue.$t("titles.error"),
                    message: message,
                    type: "error"
                })
            }
            break;
        case PARAMS.gitMethods.query://地图列表
            if(level === 0){
                let mapList = message.split(":");
                store.commit("mapList", mapList);
                store.commit('setNowMapName', mapList[0]);
            }else if(level === 1){
                noticeAndMessige({
                    title: vue.$t("titles.error"),
                    message: message,
                    type: "error"
                })
            }
            break;
        default:
            break;
    }
}